package com.walixiwa.player.widget;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.dueeeke.videoplayer.controller.ControlWrapper;
import com.dueeeke.videoplayer.controller.IControlComponent;
import com.walixiwa.videoviewcontral.R;

import java.util.ArrayList;
import java.util.List;


public class DetailVodEpisodeView extends FrameLayout implements IControlComponent {
    private ControlWrapper mControlWrapper;
    private RecyclerView mRvEpisode;

    private List<String> vodNameList = new ArrayList<>();
    private int checkedEpisode = 0;

    public DetailVodEpisodeView(@NonNull Context context) {
        super(context);
        initView();
    }

    public DetailVodEpisodeView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public DetailVodEpisodeView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }


    private void initView() {
        this.setVisibility(GONE);
        LayoutInflater.from(getContext()).inflate(R.layout.detail_layout_episode_view, this, true);
        mRvEpisode = findViewById(R.id.rv_episode);
        mRvEpisode.setAdapter(adapter);
    }

    private RecyclerView.Adapter adapter = new RecyclerView.Adapter<MyViewHolder>() {
        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View itemView = View.inflate(getContext(), R.layout.item_episode, null);
            return new MyViewHolder(itemView);
        }

        @Override
        public void onBindViewHolder(@NonNull final MyViewHolder holder, int position) {
            int checkedColor = getResources().getColor(R.color.colorPrimary);
            int defaultColor = Color.WHITE;
            holder.mTvName.setText(vodNameList.get(holder.getAdapterPosition()));
            holder.mTvName.setTextColor(checkedEpisode == holder.getAdapterPosition() ? checkedColor : defaultColor);
            holder.itemView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(holder.getAdapterPosition());
                        //setChecked(holder.getAdapterPosition());
                    }
                    setVisibility(GONE);
                }
            });
        }

        @Override
        public int getItemCount() {
            return vodNameList.size();
        }
    };

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView mTvName;

        private MyViewHolder(View viewRoot) {
            super(viewRoot);
            mTvName = viewRoot.findViewById(R.id.tv_name);
        }
    }

    @Override
    public void attach(@NonNull ControlWrapper controlWrapper) {
        mControlWrapper = controlWrapper;
    }

    @Override
    public View getView() {
        return this;
    }

    @Override
    public void onVisibilityChanged(boolean isVisible, Animation anim) {
        if (isVisible) {
            setVisibility(GONE);
        }
    }

    @Override
    public void onPlayStateChanged(int playState) {
        setVisibility(GONE);
    }

    @Override
    public void onPlayerStateChanged(int playerState) {
        setVisibility(GONE);
    }


    @Override
    public void setProgress(int duration, int position) {

    }

    @Override
    public void onLockStateChanged(boolean isLocked) {

    }


    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    public void setChecked(int position) {
        this.checkedEpisode = position;
        this.adapter.notifyDataSetChanged();
        this.mRvEpisode.scrollToPosition(position);
        //Toast.makeText(getContext(), String.valueOf(position), Toast.LENGTH_SHORT).show();
    }

    public void setVodNameList(List<String> vodNameList) {
        this.vodNameList = vodNameList;
        this.adapter.notifyDataSetChanged();
    }

    public void addVodName(String vodName) {
        this.vodNameList.add(vodName);
        this.adapter.notifyItemInserted(vodNameList.size() - 1);
    }

    public void show() {
        mControlWrapper.hide();
        this.setVisibility(VISIBLE);
        bringToFront();
    }
}
